$(function(){

    $("[name='tyfzhsys_tree']").tree({
        url:$.webPath() + '/cwgl/kmys/tyfzhsmc/get_tyfzhsmctree',
        onSelect:function(node){
            tyfzhsys_search_node(node);
        }
    });




    var tyfzhsmc = $("[name='tyfzhsys_tree']").tree("getSelected");
    $('#grid_tyfzhsys').treegrid({
        url:$.webPath() + "/cwgl/kmys/tyfzhsys/tyfzhsysList",
        toolbar:"#tyfzhsysToolBar",
        iconCls: 'icon-ok',
        fitColumns: true,
        singleSelect: true,
//        pagination: true,
        fit: true,
        rownumbers: true,
        idField: 'id',
        treeField: 'mc',
        columns:[[
            {field: 'mc', title: '名称', width: 20},
            {field: 'code', title: '代码', width: 20},
            {field: 'zjm', title: '助记码', width: 20}
        ]]
    });


    //查询按钮
    $("#tyfzhsys_search_btn").click(function () {
        tyfzhsys_search();
    });

    //设置编码范围
    $("#btn_tyfzhsys_sz").click(function () {
        tyfzhsys_sz("通用辅助核算映射科目代码配置","sz_tyfzhsys");
    });


    //新增按钮
    $("#btn_tyfzhsys_add").click(function () {
        tyfzhsys_add('新增通用辅助核算映射', 'add_tyfzhsys');
    });

    //修改按钮
    $("#btn_tyfzhsys_mod").click(function () {
        tyfzhsys_modify('修改通用辅助核算映射', 'update_tyfzhsys');
    });

    //删除按钮
    $("#btn_tyfzhsys_del").click(function () {
        tyfzhsys_delete();
    });

});



//新增
function tyfzhsys_add(title, action) {
    var tyfzhsmc = $("[name='tyfzhsys_tree']").tree("getSelected");
    if(!tyfzhsmc){
        $.messager.alert("提示", "请选择通用辅助核算名称!", "warning");
        return;
    }
    if(isNaN(tyfzhsmc.id)){
        $.messager.alert("提示", "请选择最末级名称!", "warning");
        return;
    }
    var href = $.webPath() + "/cwgl/kmys/tyfzhsys/tyfzhsys_content?tyfzhsmcId="+tyfzhsmc.id;
    var actionUrl = $.webPath() + "/cwgl/kmys/tyfzhsys/add_tyfzhsys";
    tyfzhsys_showDialog(title, href, actionUrl);
}

//修改
function tyfzhsys_modify(title, action) {
    var record = $("#grid_tyfzhsys").treegrid("getSelected");
    var tyfzhsmc = $("[name='tyfzhsys_tree']").tree("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/cwgl/kmys/tyfzhsys/tyfzhsys_content?tyfzhsysId=" + record.id+"&&tyfzhsmcId="+tyfzhsmc.id;
    var actionUrl = $.webPath() + "/cwgl/kmys/tyfzhsys/" + action;
    tyfzhsys_showDialog(title, href, actionUrl);
}

//删除
function tyfzhsys_delete() {
    var row = $('#grid_tyfzhsys').treegrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该映射？', function (r) {
            if (r) {
                var index = $('#grid_tyfzhsys').treegrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/kmys/tyfzhsys/delete_tyfzhsys",
                    dataType: 'json',
                    type: 'POST',
                    data: {tyfzhsysId: row.id},
                    beforeSend: function () {
                        $("#tyfzhsys_layout").mask();
                    },
                    success: function (data) {
                        $("#tyfzhsys_layout").mask("hide");
                        if (data.statusCode == 200) {
                            tyfzhsys_search();
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的付款单！", "warning");
    }
}

//设置编码范围
function tyfzhsys_sz(title, action) {
    var row = $('#grid_tyfzhsys').treegrid('getSelected');
    if(!row){
        $.messager.alert("提示", "请选择通用辅助核算映射!", "warning");
        return;
    }
    var href = $.webPath() + "/cwgl/kmys/tyfzhsys/tyfzhsys_sz?tyfzhsysId="+row.id;
    var actionUrl = $.webPath() + "/cwgl/kmys/tyfzhsys/sz_tyfzhsys";
    tyfzhsys_showszDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function tyfzhsys_showszDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:650,
        height:300,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    var rows = $("#grid_cz_tyfzhsys").datagrid("getChecked");
                    var idS = "";
                    for(var i = 0;i<rows.length;i++){
                        idS = idS+ rows[i].id+",";
                    }
                    idS = idS.substring(0,idS.length-1);
                    var tyfzhsysId = $("#tyfzhsys_id").val();
                    $.post($.webPath() + "/cwgl/kmys/tyfzhsys/sz_tyfzhsys",
                        { szIds: idS,tyfzhsysId:tyfzhsysId},
                    function (data) {
                        $(win).mask('hide');
                        if (data== '通用辅助核算设置成功!') {
                            $(win).window('close');
                            tyfzhsys_search();
                            $.messager.show({
                                title:'提示',
                                msg:data,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else{
                            $('#grid_tyfzhsys').treegrid('reload');
                            $.messager.alert('提示', data, 'error');
                        }
                    },
                    "text"
                    );
                }
            }
        ]
    });
}



//弹出新增或修改的窗口
function tyfzhsys_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:650,
        height:300,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'确认',
                handler:function () {
                    $(win).mask();
                    $('#tyfzhsys_Form').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            if (data== '修改通用辅助核算映射成功!' || data=='新增通用辅助核算映射成功!') {
                                $(win).window('close');
                                tyfzhsys_search();
                                $.messager.show({
                                    title:'提示',
                                    msg:data,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else{
                                $('#grid_tyfzhsys').treegrid('reload');
                                $.messager.alert('提示', data, 'error');
                            }

                        }
                    });
                }
            }
        ]
    });
}

//查询
function tyfzhsys_search() {
    var tyfzhsmc = $("[name='tyfzhsys_tree']").tree("getSelected");
    var mc = $("#tyfzhsys_mc").val();
    var code = $("#tyfzhsys_code").val();
    var page = 1;
    var rows = -1;
    $.post($.webPath() + '/cwgl/kmys/tyfzhsys/tyfzhsysList',{code:code,page:1,rows:-1,mc:mc,tyfzhsmcId:tyfzhsmc.id},function(data){
        var d =data;
        $("#grid_tyfzhsys").treegrid("loadData",d);
    },"json");
}

function tyfzhsys_search_node(node) {
    var mc = $("#tyfzhsys_mc").val();
    var code = $("#tyfzhsys_code").val();
    var page = 1;
    var rows = -1;
    $.post($.webPath() + '/cwgl/kmys/tyfzhsys/tyfzhsysList',{code:code,page:1,rows:-1,mc:mc,tyfzhsmcId:node.id},function(data){
        var d =data;
        $("#grid_tyfzhsys").treegrid("loadData",d);
    },"json");
}


